﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAO;
using MaymuseMS.DAL;
using MaymuseMS.Models;

namespace MaymuseMS.Controllers {
    public class PartsDepositoryController : ReportController<PartsDepository> {

        #region ReportController Members
        protected override GenericRepository<PartsDepository> GenericRepository {
            get { return unitOfWork.PartsDepositoryRepository; }
        }

        protected override IEnumerable<object> Get(IEnumerable<PartsDepository> entities) {
            return entities.Select(e => new {
                id = e.DepositoryId,
                cell = new object[] { 
                    e.ProductValueObject.Number,
                    e.ProductValueObject.Name,
                    e.ProductValueObject.Spec,
                    e.ProductValueObject.Model,
                    e.ProductValueObject.Unit,
                    e.Total,
                    e.SafetyCount,
                    e.Remark
                }
            });
        }
        protected override IEnumerable<object> Reports(IEnumerable<PartsDepository> entities) {
            return entities.Select(e => new {
                e.ProductValueObject.Number,
                e.ProductValueObject.Name,
                e.ProductValueObject.Spec,
                e.ProductValueObject.Model,
                e.ProductValueObject.Unit,
                e.Total,
                e.SafetyCount,
                e.Remark
            });
        }
        protected override ReportHelper<PartsDepository> ReportHelper() {
            var report = base.ReportHelper();
            report.Title = "零部件库存单";
            report.Fields = new string[] { "物料编号", "物料名称", "规格", "型号", "单位", "库存总量", "安全库存", "备注" };
            report.Predicate = d => d.Total;
            return report;
        }
        #endregion

        public ActionResult Index() {
            return View();
        }

        //[HttpPost]
        //public ActionResult Index(JQGridParm parm) {
        //    string sidx = parm.Sidx;
        //    string sord = parm.Sord;
        //    int page = parm.Page;
        //    int rows = parm.Rows;

        //    int pageIndex = Convert.ToInt32(page) - 1;
        //    int pageSize = rows;
        //    int totalRecords = unitOfWork.PartsDepositoryRepository.dbSet.Count();
        //    int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

        //    var entities = unitOfWork.PartsDepositoryRepository.Get(pageIndex, pageSize, sidx, sord, parm.Filters);

        //    var enums = entities.Select(e => new {
        //        id = e.DepositoryId,
        //        cell = new object[] { 
        //            e.ProductValueObject.Number,
        //            e.ProductValueObject.Name,
        //            e.ProductValueObject.Spec,
        //            e.ProductValueObject.Model,
        //            e.ProductValueObject.Unit,
        //            e.Total,
        //            e.SafetyCount,
        //            e.Remark
        //        }
        //    });

        //    var jsonData = new {
        //        success = true,
        //        total = totalPages,
        //        page,
        //        records = totalRecords,
        //        rows = enums
        //    };
        //    return Json2(jsonData);
        //}

        [HttpPost]
        public ActionResult Add(PartsDepository partsDepository) {
            PartsDepository partsDepositoryAdd = new PartsDepository();
            if (TryUpdateModel(partsDepositoryAdd)) {
                unitOfWork.PartsDepositoryRepository.Add(partsDepositoryAdd);
                unitOfWork.Save();
                return Json2(new { success = true });
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Edit(int? id, PartsDepository PartsDepository) {
            if (id != null) {
                PartsDepository partsDepositoryEdit = unitOfWork.PartsDepositoryRepository.Get((int)id);
                if (partsDepositoryEdit != null) {
                    if (TryUpdateModel(partsDepositoryEdit)) {
                        unitOfWork.Save();
                        return Json2(new { success = true });
                    }
                }
            }
            return Json2(new { success = false, message = errorMessage });
        }

        [HttpPost]
        public ActionResult Delete(string id) {
            if (string.IsNullOrWhiteSpace(id))
                Json2(new { success = false, message = errorMessage });
            try {
                unitOfWork.PartsDepositoryRepository.Remove(id);
                unitOfWork.Save();
                return Json2(new { success = true });
            } catch (ArgumentNullException e) {
                return Json2(new { success = false, message = e.Message });
            } catch (Exception e) {
                return Json2(new { success = false, message = errorMessage });
            }
        }

    }
}
